import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;

import java.io.*;

public class GetTxt {
    public static void main(String[] args) {
        MongoCollection<Document> target = new MongoClient("192.168.1.174", 27017).getDatabase("KAMD").getCollection("KAMD_Date_2020");
        File f = new File("E:\\年报\\2020年年报_TXT");
        FindIterable<Document> documents = target.find().sort(new Document("_id", 1));
        documents.noCursorTimeout(true);
        int count = 0;
        for (Document document : documents) {

//            System.out.println(document.getString("股票代码"));
            String code = document.getString("股票代码");
            code = code.replace(".0", "");
            String text = GetContentByCode(code, f);
            document.append("股票代码", code);
            System.out.println(code);
            String leixing = document.getString("关键审计事项类型");
            String laiyuan = GetLaiyuan(text, leixing);
            document.append("来源",laiyuan);
            System.out.println(laiyuan);
//            document.remove("TEXT");
            target.updateOne(new BasicDBObject("_id", document.get("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));

            System.out.println(count++);
        }
    }

    //获得来源
    public static String GetLaiyuan(String Text, String leixing) {
        if(Text == null) return null;
        if (Text.contains("事项描述") && Text.contains(leixing)) {
            if (Text.contains(leixing)) {
                Text = Text.substring(Text.indexOf(leixing));
            }
            if (Text.contains("事项描述")) {
                Text = Text.substring(0, Text.lastIndexOf("事项描述") + 4);
            }
            if(Text.contains("事项描述")){
                Text = Text.substring(0,Text.indexOf("事项描述"));
                String result = (Text.replace(leixing,"").replace("1、","").replace("\r\n","").trim());
                if(result.length()>5){
                    return result;
                }
            }
            return null;
        }
        if (Text.contains("关键审计事项") && Text.contains(leixing)) {
            if (Text.contains(leixing)) {
                Text = Text.substring(Text.indexOf(leixing));
            }
            if (Text.contains("关键审计事项")) {
                Text = Text.substring(0, Text.lastIndexOf("关键审计事项") + 6);
            }
            if(Text.contains("关键审计事项")){
                Text = Text.substring(0,Text.indexOf("关键审计事项"));
                String result = (Text.replace(leixing,"").replace("1、","").replace("\r\n","").trim());
                if(result.length()>5){
                    return result;
                }
            }
        }
//        if(Text.contains("事项描述")&&Text.contains(leixing)){
//            String result = Text.substring(Text.indexOf(leixing));
//            result = result.substring(0,Text.lastIndexOf("事项描述")+4);
//            System.out.println(result);
////            result = result.substring(0,Text.indexOf("事项描述"));
////            return result;
//        }
//        System.out.println(Text);
        return null;
    }

    //根据股票代码获取年报内容
    public static String GetContentByCode(String code, File f) {
        File[] f_all = f.listFiles();
        for (File file : f_all) {
            if (file.getName().contains(code)) {
                return txt2String(file);
            }
        }
        return null;
    }

    public static String txt2String(File file) {
        StringBuilder result = new StringBuilder();
        try {
            InputStream is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));//构造一个BufferedReader类来读取文件
            String s;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                result.append(System.lineSeparator() + s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result2 = result.toString();
        if (result2.contains("关键审计事项")) {
            result2 = result2.substring(result2.indexOf("关键审计事项"));
        }
        return result2;
    }
}
